﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Resto.CostControl.Core.Facade.SiteModule.DataTransferObject;
using Resto.CostControl.Core.Cache;
using Resto.CostControl.Core.Domain.Model;
using Resto.CostControl.Core.Enums;

namespace Resto.CostControl.Core.Facade.DishOrderModule.DataTransferObject
{
    /// <summary>
    /// 菜肴订单DTO
    /// </summary>
    [Serializable]
    public class DishOrderDTO
    {
        #region 构造函数

        public DishOrderDTO() { }

        internal DishOrderDTO(DishOrder order)
        {
            this.ID = order.ID;
            this.OrderDate = order.OrderDate;
            this.OrderType = (DishOrderType)Enum.Parse(typeof(DishOrderType),order.OrderType);
            this.OrderState = (DishOrderState)Enum.Parse(typeof(DishOrderState), order.OrderState);

            List<DishOrderItemDTO> list = new List<DishOrderItemDTO>();
            foreach (DishOrderItem item in order.DishOrderItems)
            {
                DishOrderItemDTO itemDTO = new DishOrderItemDTO(item);
                list.Add(itemDTO);
            }

            this.Items = list.ToArray();
        }

        #endregion

        #region 基本属性

        public string ID { get; set; }

        /// <summary>
        /// 订购日期
        /// </summary>
        public DateTime OrderDate { get; set; }

        /// <summary>
        /// 项目点Id
        /// </summary>
        public string SiteId { get; set; }

        /// <summary>
        /// 项目点名称
        /// </summary>
        public string SiteName
        {
            get
            {
                SiteDTO siteDTO = SiteDTOCache.Get(SiteId);
                if (siteDTO != null)
                    return siteDTO.Name;
                else
                    return string.Empty;
            }
        }

        /// <summary>
        /// 成本总计
        /// </summary>
        public decimal TotalCost
        {
            get
            {
                if (Items == null || Items.Length == 0)
                    return 0m;

                decimal cost = 0m;
                foreach (DishOrderItemDTO item in Items)
                    cost += item.TotalCost;

                return cost;
            }
        }

        /// <summary>
        /// 售价总计
        /// </summary>
        public decimal TotalPrice
        {
            get
            {
                if (Items == null || Items.Length == 0)
                    return 0m;

                decimal price = 0m;
                foreach (DishOrderItemDTO item in Items)
                    price += item.TotalPrice;

                return price;
            }
        }

        /// <summary>
        /// 订单类型
        /// </summary>        
        public DishOrderType OrderType { get; set; }

        /// <summary>
        /// 订单项
        /// </summary>
        public DishOrderItemDTO[] Items { get; set; }

        /// <summary>
        /// 菜单状态,UI不要赋值
        /// </summary>
        public DishOrderState OrderState { get; set; }

        #endregion

        #region 方法

        internal DishOrder ToDishOrder()
        {
            DishOrder order = new DishOrder()
            {
                SiteID = this.SiteId,
                OrderType = this.OrderType.ToString(),
            };

            CopyToDishOrder(order);

            return order;

        }

        internal void CopyToDishOrder(DishOrder order)
        {
            order.TotalCost = this.TotalCost;
            order.TotalPrice = this.TotalPrice;
            order.OrderDate = this.OrderDate.Date;

            order.ClearItems();

            foreach (DishOrderItemDTO itemDTO in this.Items)
            {
                DishOrderItem item = itemDTO.ToDishOrderItem();
                order.AddOrderItem(item);
            }
        }

        #endregion
    }
}
